package com.ljy.cart.controller;

import com.ljy.cart.interceptor.CartInterceptor;
import com.ljy.cart.service.CartService;
import com.ljy.cart.vo.Cart;
import com.ljy.cart.vo.CartItem;
import com.ljy.cart.vo.UserInfoTo;
import com.ljy.common.constant.AuthServerConstant;
import com.ljy.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    /*
    * @TODO 用户跳转购物车，跳转到列表页
    *       存储user-key标识用户身份，一个月过期
    *       如果第一次使用购物车功能，都会给一个临时的用户身份
    *       浏览器以后保存，每次访问都会带上cookie
    *       登陆：session有
    *       没登陆：按照cookie中的user-key来做
    *       第一次，如果没有临时用户，还要帮忙创建临时用户。
    * @author ljy
    * @time 2022-01-29 09:45:58
    * @method cartListPage
    * @param param
    * @return java.lang.String
    */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        //查看有无登录信息 session判断

        //有 跳转 other


        //没有 创建临时身份

        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);

        return "cartList";
    }


    /*
    * @TODO 添加商品到购物车
    * @author ljy
    * @time 2022-01-29 12:52:08
    * @method
    *       RedirectAttributes.redirectAttribute("1",2)将数据放到url后进行拼接
    *       RedirectAttributes.addFlashAttribute()，一次性session，取出后销毁
    * @param param
    * @return
    */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        ra.addAttribute("skuId",skuId);

        return "redirect:https://48u4939222.eicp.vip/cart/addToCartSuccess.html";
    }


    @GetMapping("/addToCartSuccess.html")
    public String addToCartPage(@RequestParam("skuId") Long skuId,Model model){
        //重定向到成功页面，再次查询购物车数据即可
        CartItem item = cartService.getCartItem(skuId);
        model.addAttribute("item",item);
        return "success";
    }


    @ResponseBody
    @GetMapping("/checkItem")
    public void checkItem(@RequestParam("skuId") Long skuId,
                          @RequestParam("check") Integer check){
        cartService.checkItem(skuId, check);
    }

    @ResponseBody
    @GetMapping("/countItem")
    public void countItem(@RequestParam("skuId") Long skuId,
                          @RequestParam("countNum") String countNum){
        cartService.countItem(skuId, countNum);
    }


    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:https://48u4939222.eicp.vip/cart/cart.html";
    }



    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems(){

        return cartService.getUserCartItems();
    }

}
